package com.ipharma.gui;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.util.List;
import java.util.Vector;

import javax.swing.JPanel;

import com.ipharma.McdError;
import com.ipharma.McdPrs;
import com.ipharma.dto.Product;

/**
 * A Swing-based panel class.
 * <P>
 * 
 * @author JorgE.H
 */
public class GUIButtonPanel extends JPanel {

   /**
    * Layouts
    */
   GridLayout layoutBase;

   GUIButtonPanel_jButt_actionAdapter buttonAdapter = new GUIButtonPanel_jButt_actionAdapter(
         this);

   /**
    * Attributes
    */
   Vector vButtons = new Vector();

   List vContents;

   GUIOperation myContainer;

   int panelColumns;

   int panelRows;

   /**
    * Constructs a new instance.
    */
   public GUIButtonPanel(GUIOperation pOper, List pContents) {

      super();
      try {
         myContainer = pOper;
         vContents = pContents;
         panelColumns = 4;
         panelRows = 9;
         jbInit();
      } catch (Exception e) {
         McdError.add(e);
      }
   }

   /**
    * Initializes the state of this instance.
    */
   private void jbInit() throws Exception {

      GUIButtonProduct buttonProduct;
      layoutBase = new GridLayout(panelRows, panelColumns, 0, 1);
      int i = 0;
      int l_qty = 0;

      this.setLayout(layoutBase);
      this.setBackground(Colors.APN_BACK);
      String current = "";

      // populate button panel with products or empty, disabled buttons
      for (int li_row = 0; li_row < panelRows; li_row++)
         for (int li_col = 0; li_col < panelColumns; li_col++) {
            if (i < vContents.size()) {
               // creates a new product button
               Product l_prod = (Product) vContents.get(i);
               // we want to show only 1 button per product, so skip buttons for
               // repeated products
               if (current.equals(l_prod.getDescription()))
                  buttonProduct = new GUIButtonProduct(McdPrs.EMPTY, 0,
                        McdPrs.NO_CODE, false, false, i, Images.buttProduct,
                        Images.buttProductDn);
               else {
                  current = l_prod.getDescription();
                  buttonProduct = new GUIButtonProduct(
                        (Product) l_prod.clone(), true, i, Images.buttProduct,
                        Images.buttProductDn);
                  buttonProduct.addActionListener(buttonAdapter);
               }
            } else
               // creates a new empty button
               buttonProduct = new GUIButtonProduct(McdPrs.EMPTY, 0,
                     McdPrs.NO_CODE, false, false, i, Images.buttProduct,
                     Images.buttProductDn);

            // adds button to a vector
            vButtons.add(buttonProduct);

            // adds button to screen
            this.add(buttonProduct);
            i++;
         }
   }

   /**
    * Refreshes all buttons content (retornos al dispenser)
    */
   public void refresh(List pContents) {

      String current = "";
      for (int i = 0; i < pContents.size() && i < vButtons.size(); i++) {
         GUIButtonProduct l_bp = (GUIButtonProduct) vButtons.elementAt(i);
         Product l_prod = (Product) pContents.get(i);
         if (current.equals(l_prod.getDescription()))
            continue;
         current = l_prod.getDescription();
         l_bp.setProduct((Product) pContents.get(i));
      }
   }

   /**
    * Activates all buttons
    */
   public void setAllActive(boolean pActive) {

      for (int i = 0; i < vButtons.size(); i++) {
         GUIButtonProduct l_bp = (GUIButtonProduct) vButtons.elementAt(i);
         l_bp.setActive(pActive);
      }
   }

   /**
    * Activates all buttons that have a zero quantity product
    */
   public void setZeroActive(boolean pActive) {

      for (int i = 0; i < vButtons.size(); i++) {
         GUIButtonProduct l_bp = (GUIButtonProduct) vButtons.elementAt(i);
         if (!l_bp.product.getCode().equals(McdPrs.NO_CODE)
               && l_bp.product.getQuantity() == 0)
            l_bp.setActive(pActive);
      }
   }

   /**
    * Simulates that the button corresponding to pProd was pushed. This method
    * is called when barcode reader is used.
    * 
    * @param pProd
    *           the Product selected
    */
   public void virtualPushed(Product pProd) {

      GUIButtonProduct l_butt;
      Product l_prod;
      boolean lb_found = false;
      int i;

      // searches for the button of pProd
      for (i = 0; i < vButtons.size(); i++) {
         l_butt = (GUIButtonProduct) vButtons.elementAt(i);
         l_prod = l_butt.product;
         // changes label in selected product button
         // comparing to "no_code" permits adding new products to recyclebin
         if (l_prod.equals(pProd) || l_prod.getCode().equals(McdPrs.NO_CODE)) {
            l_butt.setProduct(pProd);
            break;
         }
      }
   }

   void jButt_actionPerformed(ActionEvent e) {

      int li_selQty = 0;
      GUIButtonPanel l_panel;
      GUIButtonProduct l_butt;
      Product l_prod;

      l_butt = (GUIButtonProduct) e.getSource();
      l_prod = l_butt.product;

      // selects product only if stock available
      if (l_prod.getQuantity() >= 0)
         li_selQty = myContainer.selectProduct(l_prod);

      // modifies button label, product quantity is modified in selectProduct()
      if (li_selQty > 0)
         l_butt.setProduct(l_prod);
   }
}

class GUIButtonPanel_jButt_actionAdapter implements
      java.awt.event.ActionListener {

   GUIButtonPanel adaptee;

   GUIButtonPanel_jButt_actionAdapter(GUIButtonPanel adaptee) {

      this.adaptee = adaptee;
   }

   public void actionPerformed(ActionEvent e) {

      adaptee.jButt_actionPerformed(e);
   }
}
